package repository

import (
	"ginEssential/common"
	"ginEssential/model"
	"github.com/jinzhu/gorm"
)

type CategoryRepository struct {
	DB *gorm.DB
}
func NewCategoryRepository() CategoryRepository{
	return CategoryRepository{
		DB: common.GetDB(),
	}
}
func (receiver CategoryRepository) Create(name string) (*model.Category,error) {
	category:=model.Category{Name: name}
	if err:=receiver.DB.Create(&category).Error;err!=nil{
		return nil,err
	}
	return &category,nil
}
func (receiver CategoryRepository) Update(category model.Category ,name string) (*model.Category,error)  {

	if err:=receiver.DB.Model(&category).Update("name",name).Error;err!=nil{
		return nil,err
	}
	return &category,nil
}
func (receiver CategoryRepository) SelectById(id interface{}) (*model.Category,error) {
	var category model.Category
	if err:=receiver.DB.First(&category,id).Error;err!=nil {
		return nil,err
	}
	return &category,nil
}
func (receiver CategoryRepository) DeleteById(id interface{}) error {
	var category model.Category
	if err:=receiver.DB.First(&category,id).Error;err!=nil {
		return err
	}
	receiver.DB.Delete(&category)
	return nil
}